package cn.wolfcode.handler;

import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.redis.SeckillRedisKey;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

/**
 * canal
 * 问题
 *      会将null作为空串保存, 因此对于无法用空串直接转换的包装类型, 要设置默认值, 防止抛异常
 *      类字段如果和表中列名不一样, 需要用JPA注解覆盖
 */
@Component
// 监听 t_order_info 表
@CanalTable("t_order_info")
public class OrderInfoHandler implements EntryHandler<OrderInfo> {
    @Autowired
    private StringRedisTemplate redisTemplate;

    public void insert(OrderInfo orderInfo) {
        System.out.println("订单新增了");
        // 增加订单时, 缓存 hash               seckillOrderHash:           orderNo        orderInfo
        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_ORDER_HASH.getRealKey(""), orderInfo.getOrderNo(), JSON.toJSONString(orderInfo));
    }

    // before 只有被更改字段有值;    after 是所有都有值, 更改后的记录
    public void update(OrderInfo before, OrderInfo after) {
        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_ORDER_HASH.getRealKey(""), after.getOrderNo(), JSON.toJSONString(after));
        System.out.println("订单更新了");
    }

    public void delete(OrderInfo orderInfo) {
        System.out.println("订单删除了");
    }

}
